package com.bocai.eelly.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bocai.eelly.R;
import com.bocai.eelly.common.BaseFragment;
import com.trello.rxlifecycle.components.support.RxFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HomeFragment extends BaseFragment {
    @Bind(R.id.rb_home_one)
    RadioButton rbHomeOne;
    @Bind(R.id.rb_home_two)
    RadioButton rbHomeTwo;
    @Bind(R.id.rb_home_three)
    RadioButton rbHomeThree;
    @Bind(R.id.rg_home)
    RadioGroup rgHome;
    @Bind(R.id.fl_home)
    FrameLayout flHome;
    private View view;
    private RxFragment purchaserFrag;
    private RxFragment orderFrag;
    private RxFragment supplierFtag;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, null);
        initView();
        ButterKnife.bind(this, view);
        return view;

    }

    private void initView() {
        ButterKnife.bind(this, view);
        setSelect(0);
        initEvent();

    }

    private void initEvent() {
        rgHome.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.rb_home_one:
                        rbHomeOne.setTextColor(getResources().getColor(R.color.cl_white));
                        rbHomeTwo.setTextColor(getResources().getColor(R.color.cl_text_unselecte));
                        rbHomeThree.setTextColor(getResources().getColor(R.color.cl_text_unselecte));
                        setSelect(0);
                        break;
                    case R.id.rb_home_two:
                        rbHomeOne.setTextColor(getResources().getColor(R.color.cl_text_unselecte));
                        rbHomeTwo.setTextColor(getResources().getColor(R.color.cl_white));
                        rbHomeThree.setTextColor(getResources().getColor(R.color.cl_text_unselecte));
                        setSelect(1);
                        break;
                    case R.id.rb_home_three:
                        rbHomeOne.setTextColor(getResources().getColor(R.color.cl_text_unselecte));
                        rbHomeTwo.setTextColor(getResources().getColor(R.color.cl_text_unselecte));
                        rbHomeThree.setTextColor(getResources().getColor(R.color.cl_white));
                        setSelect(2);
                        break;
                }
            }
        });
    }

    public void setSelect(int i) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        switch (i) {
            case 0:
                hideFragment(transaction);
                if (purchaserFrag == null) {
                    purchaserFrag = new PurchaserFragment();
                    transaction.add(R.id.fl_home, purchaserFrag);
                } else {
                    transaction.show(purchaserFrag);
                }
                break;
            case 1:
                hideFragment(transaction);
                if (supplierFtag == null) {
                    supplierFtag = new SupplierFragment();
                    transaction.add(R.id.fl_home, supplierFtag);
                } else {
                    transaction.show(supplierFtag);
                }
                break;
            case 2:
                hideFragment(transaction);
                if (orderFrag == null) {
                    orderFrag = new OrderFragment();
                    transaction.add(R.id.fl_home, orderFrag);
                } else {
                    transaction.show(orderFrag);
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }
    private void hideFragment(FragmentTransaction transaction) {
        if (purchaserFrag != null) {
            transaction.hide(purchaserFrag);
        }
        if (supplierFtag != null) {
            transaction.hide(supplierFtag);
        }
        if (orderFrag != null) {
            transaction.hide(orderFrag);
        }


    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
